package es.cazadoresmadridejos.beans.controller.deer;

import javax.servlet.http.HttpServletRequest;

import org.displaytag.pagination.PaginatedList;
import org.displaytag.properties.SortOrderEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import es.cazadoresmadridejos.beans.manager.deer.DeerManager;
import es.cazadoresmadridejos.beans.manager.session.UserPreferences;
import es.cazadoresmadridejos.objs.web.Page;

@Controller
public class DeerListController {
	
	@Autowired private DeerManager deerManager;
	@Autowired(required=false) private UserPreferences userPreferences;

	@RequestMapping(value="/deerList*.html")
	public ModelAndView list(
			HttpServletRequest request,
			Boolean isList
			) throws Throwable {

		ModelAndView mv;
		if(Boolean.TRUE.equals(isList)){
			mv = new ModelAndView("deerList");
		}else{
			mv = new ModelAndView();
		}		
		
		String dir = request.getParameter("dir");
		Page page = new Page(request);
		if(dir == null){
			page.setSortOrder(SortOrderEnum.DESCENDING);
		}

		PaginatedList list = deerManager.findAllPaginated(page,userPreferences.getPartner().getId());

		mv.addObject("list", list);

		return mv;
	}

}